<?php
include_once('init.php'); //this file does stuff like initializes the facebook object etc


$notificationName=get_notification_name($appname);
$message=get_request_message($appname);
$image='http://thedevbook.com/apps/qmark_tiny.jpg';

if ($_POST['f']) {
$triedmore=0;
$MAX_FRIENDS_PER_REQUEST=10;
$uid=$_POST['user'];
$ses=$_POST['session'];

$values = $_POST['f'];


if (count($values) > 10)
{
    $triedmore=1;
}elseif (count($values)==0)
{
    echo "You didn't add any friends";

    //$facebook->redirect(get_app_url($appname));
}
    
$ids=implode(',',array_slice($values,0,$MAX_FRIENDS_PER_REQUEST));



//  public function notifications_sendRequest($to_ids, $type, $content, $image, $invite)
$redirurl=$facebook->api_client->notifications_sendRequest($ids, $notificationName ,$message,$image,'false');
//echo $redirurl;

if ($redirurl==''){
        //storeinvite($uid,$ses,$ids,'failed',$triedmore,time()); you'd need this function for this to work
    $facebook->redirect(get_app_url($appname));

}else{
        //storeinvite($uid,$ses,$ids,'success',$triedmore,time()); you'd need this function for this to work
$facebook->redirect($redirurl);

}
//print_r($ids);

}
else{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <title>yeah a title would be nice</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <script type="text/javascript" src="../js/basics.js"></script>
        <script type="text/javascript" src="../js/facebook.js"></script>        
        <link href="../css/facebook.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
        <?  include('header.php'); ?>
        <br />
        <br />
        <div align="center">
        Below are your friends who haven't yet added this application <br /><br />
            <? print_invite_form($facebook,$user,$facebook->api_client->session_key,$appname);?>
        </div>
        <? include('footer.php'); ?>
    </body>
</html>

<?

}


function print_invite_form($facebook,$uid,$ses,$app){

    $rawfriends=$facebook->api_client->friends_get();
    if (count($rawfriends) > 1) {
        $friends = implode(',',$rawfriends);
        $friendlist = "'" . str_replace(",","','",$friends) . "'";
        // might error if 0 or 1
        $picturearray = $facebook->api_client->fql_query("SELECT name,first_name, last_name, pic_square,uid FROM user WHERE uid in ($friendlist) AND has_added_app='0';");
        usort($picturearray,'CompareFriendsNames');

        echo "<form method=\"post\" >
        <div class=\"sharesubmit\">
        
        <input type=\"submit\" value=\"Invite Your Friends\" class=\"inputsubmit donebutton\"/><br />
        Please only invite up to 10 friends at a time.
        </div>
        <div class=\"selectall\" style=\"width:500px;\">
        <a href=\"#\" onclick=\"selectAll(true);return false;\"> Select All </a>
        |
        <a href=\"#\" onclick=\"selectAll(false);return false;\"> Unselect All </a>
        </div>

        <input type=\"hidden\" name=\"user\" value=\"$uid\"/>
        <input type=\"hidden\" name=\"session\" value=\"$ses\"/>
        <input type=\"hidden\" name=\"app\" value=\"$app\"/>
        <input type=\"hidden\" name=\"pcid\" value=\"0\"/>
        <table>
        <tr>
        <td>
        <div class=\"friend_selector\" style=\"height:375px;width:500px;overflow:auto;\">
        <table>";

        //echo friends tables here
        $x=0;
        for ($i=0;$i < count($picturearray);$i++){

            if ($x <= 0){
                echo "<tr>\n";
            }

            echo '<td class="friendtile">    ';
            if     (strlen($picturearray[$i][pic_square])==0){
                $picturearray[$i][pic_square]=APPBASE . 'images/qmark_tiny.jpg';
            }
            echo "<table><tr><td><img src=\"" . $picturearray[$i][pic_square].
            "\" style=\"width:30px;\" alt=\"userpic\" /></td><td><input id=\"check_$i\" " .
            " type=\"checkbox\" name=\"f[]\" value=\"" .
            $picturearray[$i][uid] . "\" checked=\"checked\" /></td><td><span class=\"friendtext\">" .
            $picturearray[$i][name] . "</span></td></tr></table>";
            echo '</td>';
            if ($x >= 2){
                echo "</tr>\n";
                $x=0;

                }elseif ($i== (count($picturearray)-1)){
                    echo "</tr>\n";
                    }else{
                        $x++;
                    }
                }


                echo'                        </table>
                </div>
                </td>
                </tr>
                <tr>
                <td>
                <div class="sharesubmit">
                <input type="submit" value="Invite Your Friends" class="inputsubmit donebutton"/>

                </div>
                </td>
                </tr>
                </table>
                </form>';

            }
        }
?> 